<template>
    <div class="loginview">
        <div class="panel login-panel">
            <div class="panel-header">
                <h1 class="panel-title">共派后台管理系统</h1>
                <!--<h3 class="panel-title2">请输入账号密码登录</h3>-->
            </div>
            <div class="panel-body">
                <el-form :model="params" :rules="rules" ref="loginForm" >
                    <el-form-item prop="phone">
                        <el-input v-model="params.phone"  placeholder="手机号" maxlength="11"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="params.password" type="password"  placeholder="密码"  minlength="6" maxlength="18"></el-input>
                    </el-form-item>
                    <el-form-item class="login-button">
                        <el-button type="warning" plain class="pull-right" style="width: 100%" @click="loginFormSubmit()" >登录</el-button>
                    </el-form-item>
                    <div class="tips">
                        <p>科零 版权所有 ©2018</p>
                    </div>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>

    import { mapActions, mapState, mapGetters, mapMutations } from 'vuex'

    export default {
        name: "login",
        data(){
            let checkMobile = (rule, value, callback) => {
                let phone=/^[1][3,4,5,7,8][0-9]{9}$/;
                if (!phone.test(value)) {
                    callback(new Error('请输入正确的手机号'));
                } else {
                    callback();
                }
            };
            return {
                params:{
                    phone:"",
                    password:"",
                },
                rules: {
                    phone: [
                        {validator: checkMobile, trigger: 'blur'}
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 25, message: '长度在 6 到 18 个字符之间', trigger: 'blur' }
                    ]
                }
            }
        },
        methods:{
            ...mapMutations([
                'SET_LOGIN_FAILURE',
            ]),
            ...mapActions([
                'login',
                'loginTest',
                // 'fetchProfile'
            ]),
            loginFormSubmit()
            {
                this.$refs['loginForm'].validate((valid) =>{
                    if(valid){
                        // if(this.params.phone=="15902082831"){
                        //     this.loginTest();
                        // }else{
                            this.login(this.params);
                        // }
                    }else{
                        return false;
                    }
                });
            }
        },
        watch:{
            access_token () {
                this.$router.replace({
                    path: '/admin/member'
                })
            },
            loginFailure (value) {
                if (value) {
                    this.$message.error(value);
                    this.loading = false;
                }
                this.SET_LOGIN_FAILURE();
            }
        },
        computed: {
            ...mapState ({
                access_token: state => state.account.access_token,
                user: state => state.account.user,
                error: state => state.account.loginError,
                loginFailure: state => state.account.loginFailure
            }),
            ...mapGetters ([
                'menus'
            ])
        }
    }
</script>

<style lang="scss">
    .loginview {
        position: fixed;
        width: 100%;
        background: rgba(248,154,55,0.5);
        height: 100%;
        .panel-title {
            margin: 0 0 40px 0;
        }
    }
    .login-panel {
        background-color: #FFF;
        padding: 40px;
        border-radius: 4px;
        box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
        width: 400px;
        display: inline-block;
        max-width: 100%;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -200px;
        transform: translateY(-50%);
        z-index: 10;
        .panel-header {
            text-align: center;
            .panel-title {
                color: #76838f;
                margin-bottom: 10px;
            }
            .panel-title2{
                color: #76838f;
                font-size: 14px;
                margin-bottom: 30px;
            }
        }
        .panel-body {
            text-align: center;
            padding: 10px;
            .tips {
                font-size: 12px;
                padding-top: 20px;
            }
        }
        .login-button {
            padding-top: 10px;
        }
    }
</style>
